home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gnulib / libsrc98.zoo / new_pipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-19  |  2.6 KB  |  129 lines

  1. /* pipe.c from edgars lib */
  2.  
  3. #include <stat.h>
  4. #include <device.h>
  5. #include <stddef.h>
  6. #include <types.h>
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <macros.h>
  11.  
  12. #ifndef mkdev
  13. #define mkdev(a,b)    ((((a) << 8) & 0xff00)|((b) & 0x00ff))
  14. #endif
  15.  
  16.  
  17. static size_t    pipe_size;
  18. static size_t    pipe_allocated = 0;
  19. static long    pipe_reading_pos, pipe_writing_pos;
  20. static char    *pipe_buffer = NULL;
  21. static int    pipe_is_open = 0;
  22.  
  23. static long
  24. pipe_open(const char *name, int flags, unsigned mode)
  25. {
  26.     return(mkdev('P', 0));
  27. }
  28.  
  29. static long
  30. pipe_close(int fd)
  31. {
  32.     if(pipe_is_open) pipe_is_open--;
  33.     return(0);
  34. }
  35.  
  36. static long
  37. pipe_read(int fd, void *buf, long nbytes)
  38. {
  39.     long    new_pos, ret;
  40.  
  41.     new_pos = min(pipe_size, pipe_reading_pos + nbytes);
  42.     ret = new_pos - pipe_reading_pos;
  43.     if(ret) bcopy(pipe_buffer + pipe_reading_pos, buf, ret);
  44.     pipe_reading_pos = new_pos;
  45.     return(ret);
  46. }
  47.  
  48. static long
  49. pipe_write(int fd, void *buf, long nbytes)
  50. {
  51.     long    new_pos, ret;
  52.  
  53.     new_pos = min(pipe_allocated, pipe_writing_pos + nbytes);
  54.     ret = new_pos - pipe_writing_pos;
  55.     if(ret) bcopy(buf, pipe_buffer + pipe_writing_pos, ret);
  56.     pipe_writing_pos = new_pos;
  57.     pipe_size = max(new_pos, pipe_size);
  58.     return(ret);
  59. }
  60.  
  61. static long
  62. pipe_seek(int fd, long where, int how)
  63. {
  64.     if(how == SEEK_CUR) where += pipe_reading_pos;
  65.     else if(how == SEEK_END) where = pipe_size - where;
  66.     where = max(where, 0);
  67.     where = min(where, pipe_size);
  68.     return(pipe_reading_pos = where);
  69. }
  70.  
  71. static int
  72. pipe_fstat(int fd, struct stat *st)
  73. {
  74.     st->st_blksize = 1024;
  75.     st->st_size = pipe_size;
  76.     st->st_blocks = (pipe_size + 1023) / 1024;
  77.     st->st_ino = fd;
  78.     st->st_rdev = fd;
  79.     st->st_mode = S_IFCHR | 0600;
  80.     st->st_attr = 0x80;
  81.     st->st_ino = st->st_rdev = fd;
  82.     st->st_mtime = st->st_ctime = st->st_atime = 
  83.         time((time_t *)0) - 2;
  84.     st->st_nlink = 2;
  85.     st->st_uid = geteuid();
  86.     st->st_gid = getegid();
  87.     return 0;
  88. }
  89.  
  90. #define pipe_ioctl    NULL
  91.  
  92. extern struct _device    *__devices;
  93.  
  94. struct _device    pipe_dev =
  95.     {"PIPE:", "pipe", mkdev('P', 0),
  96.          pipe_open, pipe_close, pipe_read, pipe_write, pipe_ioctl, NULL };
  97.  
  98. static void
  99. _init_pipes()
  100. {
  101.     struct _device    *dev;
  102.  
  103.     for(dev = __devices; dev; dev = dev->next)
  104.         if(dev->dev == mkdev('P', 0)) return;
  105.     pipe_dev.next = __devices;
  106.     __devices = &pipe_dev;
  107.     pipe_allocated = 8 * 1024L;
  108.     if(!(pipe_buffer = malloc(pipe_allocated))) pipe_allocated = 0;
  109. }
  110.  
  111. static void
  112. _exit_pipes()
  113. {
  114.     return;
  115. }
  116.  
  117. int
  118. pipe(fildes)
  119. int    fildes[2];
  120. {
  121.     if(pipe_is_open) return -1; /* can't make this pipe */
  122.     _init_pipes();
  123.     pipe_size = pipe_reading_pos = pipe_writing_pos = 0;
  124.     pipe_is_open = 2;
  125.     fildes[0] = fildes[1] = mkdev('P', 0);
  126.     return 0;
  127. }
  128.  
  129.